//
//  GuideViewController.h
//  hehenian-mobile
//
//  Created by huihu on 15/7/10.
//  Copyright (c) 2015年 huihu. All rights reserved.

#import "GuideViewController.h"
#import "AppDelegate.h"

#import "FingerView.h"
#import "DHNavigationViewController.h"


#import "SplashView.h"

#define KNumberPage 4
@interface GuideViewController()

@property(assign,nonatomic,getter=isloadedFourth) BOOL loadedFourth;
/**
 *  第4张引导图
 */
@property(nonatomic,weak) UIView *image_view4;
@property(nonatomic,weak) FingerView *fingerView;

@end

@implementation GuideViewController
@synthesize imageView;
@synthesize left = _left;
@synthesize right = _right;
@synthesize pageScroll;
@synthesize pageControl;



#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    pageScroll =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    pageControl =[[UIPageControl alloc]initWithFrame:CGRectMake((self.view.frame.size.width-36)*0.5,self.view.frame.size.height-36, 38, 36)];
    pageControl.currentPageIndicatorTintColor=[UIColor redColor];
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    
    pageScroll.showsHorizontalScrollIndicator=NO;
    pageScroll.showsVerticalScrollIndicator=NO;
    pageControl.numberOfPages = KNumberPage;
    pageControl.currentPage = 0;
    pageScroll.delegate = self;
    pageScroll.contentSize = CGSizeMake(self.view.frame.size.width * KNumberPage, self.view.frame.size.height);
    pageScroll.scrollsToTop = YES;
    pageScroll.bouncesZoom = YES;
    pageScroll.pagingEnabled = YES;
    pageScroll.scrollEnabled = YES;
    pageScroll.bouncesZoom = YES;
    pageScroll.canCancelContentTouches = YES;
    pageScroll.opaque = YES;
    
    self.navigationController.navigationBar.hidden = YES;
    UIImageView *image_view1 =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guid1"]];
    [image_view1 setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [pageScroll addSubview:image_view1];
    UIImageView *image_view2 =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guid2"]];
    [image_view2 setFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    [pageScroll addSubview:image_view2];
    UIImageView *image_view3 =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guid3"]];
    [image_view3 setFrame:CGRectMake(self.view.frame.size.width*2, 0, self.view.frame.size.width, self.view.frame.size.height)];
     [pageScroll addSubview:image_view3];
    
//    UIView *image_view4=[UIView new];
//    image_view4.backgroundColor=[UIColor whiteColor];
    UIImageView *image_view4 =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guid4"]];
//    self.image_view4=image_view4;
    [image_view4 setFrame:CGRectMake(self.view.frame.size.width*3, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [pageScroll addSubview:image_view4];
    
    _gotoMainViewBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [_gotoMainViewBtn setFrame:CGRectMake(self.view.frame.size.width*3, 0, ScreenWidth, ScreenHeight)];
    _gotoMainViewBtn.backgroundColor=[UIColor clearColor];
    [self.view bringSubviewToFront:_gotoMainViewBtn];
    [_gotoMainViewBtn addTarget:self action:@selector(gotoMainView:) forControlEvents:UIControlEventTouchUpInside];
    [pageScroll addSubview:_gotoMainViewBtn];

    [self.view addSubview:pageScroll];
    [self.view addSubview:pageControl];
    pageControl.autoresizingMask=UIViewAutoresizingFlexibleTopMargin;
}

- (void)viewDidUnload
{
    [self setGotoMainViewBtn:nil];
    [super viewDidUnload];
}

/**
 *  进入首页
 */
-(void)enterIntoHome
{
    NSString *key = @"CFBundleVersion";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[HHNUtility appVersion] forKey:key];
    
    // 显示状态栏
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    SplashView *splashView=[[SplashView alloc]init];
    [splashView loadSplashView];
}


- (void)gotoMainView:(UIButton *) sendr{
    
    [UIView animateWithDuration:0.5 animations:^{
      [self btnTouch];
        
    } completion:^(BOOL finished) {
        [self enterIntoHome];
    }];
 
}



-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{	
    CGFloat pageWidth = self.view.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    
    //HHLog(@"scrollView.contentOffset.x,%.2f",scrollView.contentOffset.x);
    if (scrollView.contentOffset.x >page*pageWidth+40&&page==3) {
        [self enterIntoHome];
        return;
    }
    
    pageControl.currentPage = page;
    
    if (page==3&&!self.isloadedFourth) {
        _loadedFourth=YES;
        //第4张 动画
        HHLog(@"加载第4张");
         float time=0.7f;
        
        UIImageView *view1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guid4_1"]];
        view1.frame=self.view.frame;
        [_image_view4 addSubview:view1];
        view1.alpha=0;
        [UIView animateWithDuration:time animations:^{
            view1.alpha=1;
        } completion:^(BOOL finished) {
            
            FingerView *fingerView=[FingerView new];
            fingerView.frame=self.view.frame;
            self.fingerView=fingerView;
            [self.image_view4.layer addSublayer:fingerView];
            
            CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];
            animation.fromValue=[NSNumber numberWithFloat:0.0];
            animation.toValue=[NSNumber numberWithFloat:1.0];
            //animation.autoreverses=YES;
            animation.duration=time;
            //animation.repeatCount=FLT_MAX;
            
            animation.removedOnCompletion=NO;
            animation.fillMode=kCAFillModeForwards;
            
            [self.fingerView addAnimation:animation forKey:@"opacity"];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                UIImageView *view3=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guid4_3"]];
                view3.frame=self.view.frame;
                [_image_view4 addSubview:view3];
                view3.alpha=0;
                [UIView animateWithDuration:1.0 animations:^{
                    view3.alpha=1;
                }];
            });
            
        }];
        
        
    }
    
}
/**
 *  执行手指按压动画
 */
-(void)btnTouch
{
    // 2.创建缩放动画对象
    CABasicAnimation *scale = [CABasicAnimation animation];
    scale.keyPath = @"transform.scale";
    scale.toValue=[NSNumber numberWithFloat:0.65];
    
    // 3.平移动画
    CABasicAnimation *move = [CABasicAnimation animation];
    move.keyPath = @"transform.translation";
    move.toValue = [NSValue valueWithCGPoint:CGPointMake(-7, -7)];
    
    // 4.将所有的动画添加到动画组中
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[scale,move];
    group.duration = 0.5;
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    
    [self.fingerView addAnimation:group forKey:nil];
}


@end
